package zx.x.modules.app.utils;

public class IDCardValidator {

    private static final int[] WEIGHTS = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
    private static final char[] CHECK_CODES = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

    public static boolean validate(String idCard) {
        // 验证身份证号的长度
        if (idCard.length() != 18) {
            return false;
        }

        // 验证身份证号的前17位是否为数字
        for (int i = 0; i < 17; i++) {
            if (!Character.isDigit(idCard.charAt(i))) {
                return false;
            }
        }

        // 计算校验码
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            sum += (idCard.charAt(i) - '0') * WEIGHTS[i];
        }
        int remainder = sum % 11;
        char checkCode = CHECK_CODES[remainder];

        // 验证校验码
        return checkCode == idCard.charAt(17);
    }

    public static void main(String[] args) {
        String idCard = "44052419940101001X";
        if (validate(idCard)) {
            System.out.println("身份证号有效");
        } else {
            System.out.println("身份证号无效");
        }
    }
}

